package leetcode;


import java.util.ArrayList;
import java.util.List;

public class l0023_合并K个升序链表_2_暴力法2_基于比较 {

    public ListNode mergeKLists(ListNode[] lists) {
        //前置校验
        if (lists == null || lists.length == 0) {
            return null;
        }
        //将n个元素用动态数组存储起来
        List<ListNode> listNodeList = new ArrayList<>();
        for (ListNode list : lists) {
            while (list != null) {
                listNodeList.add(list);
                list = list.next;
            }
        }
        //将n个元素从小到大排序
        listNodeList.sort((ListNode list1, ListNode list2) -> {
            return list1.val - list2.val;
        });
        //用一个链表将数据串起来
        ListNode dummyHead = new ListNode(0);
        ListNode cur = dummyHead;
        for (ListNode node : listNodeList) {
            cur.next = node;
            cur = cur.next;
        }
        return dummyHead.next;
    }

}
